<?php
/**
 * @author         Vladimir Popov
 * @copyright      Copyright (c) 2014 Vladimir Popov
 */

// in case redirect fails
if (Mage::registry('redirect_url')) {
    ?>
    <meta http-equiv="refresh" content="0;url=<?php echo Mage::registry('redirect_url') ?>">
    <script type="text/javascript">
        window.location = '<?php echo Mage::registry('redirect_url')?>';
    </script>
<?php
}

// get temporary data
$tmpData = Mage::getSingleton('core/session')->getData('webform_result_tmp_' . $this->getWebform()->getId());
$tmpResult = new Varien_Object();
if (!empty($tmpData['field']) && is_array($tmpData['field'])) {
    foreach ($tmpData['field'] as $field_id => $value) {
        $tmpResult->setData('field_' . $field_id, $value);
    }
}

if ($this->getWebform()->getIsActive() == VladimirPopov_WebForms_Model_Webforms::STATUS_DISABLED || !$this->isDirectAvailable()) {
    echo $this->getNotAvailableMessage();
} else {

    if (Mage::registry('show_form_name')) {
        ?>
        <div class="page-head">
            <h3><?php echo $this->getWebform()->getName(); ?></h3>
        </div>
    <?php } ?>

    <?php if ($this->isAjax()) { ?>
        <div id="webform_<?php echo $this->getWebform()->getId() ?>_success_text" class="std webforms-success-text"
             style="display:none"></div>
    <?php } ?>

    <?php if (Mage::registry('captcha_invalid')) { ?>
        <ul class="messages">
            <li class="error-msg">
                <ul>
                    <li><?php echo Mage::helper('webforms')->__('Verification code was not correct. Please try again.'); ?></li>
                </ul>
            </li>
        </ul>
    <?php } ?>

    <?php
    if (Mage::registry('show_success')) {
        ?>
        <div class="std webforms-success-text">
            <?php echo $this->getWebform()->getSuccessText() ?>
        </div>
    <?php
    } else {
        ?>

        <div id="webform_<?php echo $this->getWebform()->getId() ?>_form">

            <?php if ($this->getWebform()->getDescription()) { ?>
                <div class="std">
                    <?php echo $this->getWebform()->getDescription(); ?>
                </div>
            <?php } ?>

            <?php if ($this->isAjax()) { ?>
                <iframe id="webform_<?php echo $this->getWebform()->getId() ?>_iframe"
                        name="webform_<?php echo $this->getWebform()->getId() ?>_iframe"
                        style="width:0;height:0;border:0;position:absolute"></iframe>
            <?php } ?>


            <form action="<?php echo $this->getFormAction() ?>" method="post"
                  name="webform_<?php echo $this->getWebform()->getId() ?>"
                  id="webform_<?php echo $this->getWebform()->getId() ?>" enctype="<?php echo $this->getEnctype() ?>"
                  class="webforms-<?php echo $this->getWebform()->getCode() ?>"
                  <?php if ($this->isAjax()) { ?>target="webform_<?php echo $this->getWebform()->getId() ?>_iframe"<?php } ?>>

                <input type="hidden" name="submitWebform_<?php echo $this->getWebform()->getId() ?>" value="1"/>
                <input type="hidden" name="webform_id" value="<?php echo $this->getWebform()->getId() ?>"/>

                <?php if (is_array($this->getData('hidden'))) {
                    foreach ($this->getData('hidden') as $key => $value) {
                        ?>
                        <input type="hidden" name="<?php echo $key ?>" value="<?php echo $value ?>"/>
                    <?php
                    }
                }?>

                <?php if (count($this->getWebform()->_getHidden())) {
                    foreach ($this->getWebform()->_getHidden() as $field) {
                        echo $field->toHtml();
                    }
                } ?>

                <?php
                foreach ($this->getWebform()->_getFieldsToFieldsets() as $fieldset_id => $fieldset) {
                    ?>

                    <fieldset id="fieldset_<?php echo $fieldset_id ?>"
                              class="group-select wide fieldset-<?php echo $fieldset_id ?>"
                        <?php if ($fieldset['logic_visibility'] == 'hidden') echo 'style="display:none"'; ?>>

                        <?php if ($fieldset['name']) { ?>
                            <h2 class="legend"><?php echo $fieldset['name'] ?></h2>
                        <?php } ?>

                        <ul class="form-list">
                            <?php
                            $k = 0;
                            $j = 0;
                            $prev = false;
                            $fields_count = count($fieldset['fields']);
                            foreach ($fieldset['fields'] as $field) {

                                // set temporary value
                                $field->setData('result', $tmpResult);

                                $size_class = "fields";
                                $field_class = "input-box";
                                if ($field->getSize() == 'wide') {
                                    $size_class = "wide";
                                    $field_class = "";
                                }

                                ?>
                                <?php if ($j % 2 == 0 || $field->getSize() == 'wide' || $prev->getSize() == 'wide') {
                                    $j = 0; ?>
                                    <?php if ($k > 1) { ?></li><?php } ?>
                                    <li <?php if ($size_class == 'wide') { ?>id="field_<?php echo $field->getId() ?>_row" <?php } ?>
                                    class="<?php if ($size_class == 'wide') { ?>webforms-fields-row-<?php echo $field->getId() ?><?php } ?>"
                                    <?php if ($size_class == 'wide' && $field->getData('logic_visibility') == 'hidden') echo 'style="display:none"'; ?>>
                                <?php } ?>
                                <?php if ($size_class != "wide") { ?>
                                <div id="field_<?php echo $field->getId() ?>"
                                     class="<?php echo $field_class ?> webforms-fields-<?php echo $field->getId() ?><?php if ($field->getCode()) { ?> webforms-fields-<?php echo $field->getCode();
                                     }?>"
                                    <?php if ($field->getData('logic_visibility') == 'hidden') echo 'style="display:none"'; ?>>
                            <?php } ?>


                                <?php if ($size_class != "wide") { ?>
                                <div class="input-box">
                            <?php } ?>

                                <?php if ($field->getType() != 'html' && $field->getType() != 'subscribe') { ?>
                                    <label for="field_<?php echo $field->getId() ?>">
                                        <?php echo $field->getName() ?>
                                        <?php if ($field->getRequired()) { ?>
                                            <span class="required">*</span>
                                        <?php } ?>
                                    </label>
                                <?php } ?>

                                <br/>

                                <?php echo $field->toHtml(); ?>

                                <?php if ($size_class != "wide") { ?>
                                </div>
                            <?php } ?>

                                <?php if ($size_class != "wide") { ?>
                                </div>
                            <?php } ?>
                                <?php if ($k == $fields_count - 1) { ?>
                                    </li>
                                <?php } ?>
                                <?php
                                $k++;
                                $j++;
                                $prev = $field;
                            }?>
                        </ul>

                    </fieldset>

                <?php
                } // foreach 'fields_to_fieldsets'
                ?>

                <?php if (Mage::registry('use_captcha')) { ?>
                    <fieldset class="group-select wide">
                        <h2 class="legend">Captcha</h2>
                        <ul class="form-list">
                            <li>
                                <div class="input-box">
                                    <?php echo $this->getCaptcha()->getHTML() ?>
                                </div>
                            </li>
                        </ul>
                    </fieldset>
                <?php } ?>

                <?php if (Mage::getStoreConfig('webforms/honeypot/enable')) {
                    echo $this->getLayout()->createBlock(
                        'core/template',
                        'webform_' . $this->getWebform()->getId() . '_honeypot',
                        array(
                            'webform' => $this->getWebform(),
                            'template' => 'webforms/scripts/honeypot.phtml',
                        )
                    )->toHtml();
                }?>

                <div class="button-set">
                    <p class="required"><?php echo $this->__('* Required Fields') ?></p>
                    <button type="button" id="webform_<?php echo $this->getWebform()->getId() ?>_submit_button"
                            onclick="webform_<?php echo $this->getWebform()->getId() ?>_submit()" class="form-button"
                            title="<?php echo $this->__('submit') ?>">
                        <span><?php echo $this->__($this->getWebform()->getSubmitButtonText()) ?></span>
                    </button>
		<span class="please-wait" id="webform_<?php echo $this->getWebform()->getId() ?>_sending_data"
              style="display:none;">
			<img src="<?php echo $this->getSkinUrl('images/opc-ajax-loader.gif') ?>"
                 alt="<?php echo $this->__('Sending...') ?>" title="<?php echo $this->__('Sending...') ?>"
                 class="v-middle"/><span
                id="webform_<?php echo $this->getWebform()->getId() ?>_progress_text"><?php echo $this->__('Sending...') ?></span>
		</span>
                </div>


            </form>

        </div>

        <?php echo $this->getLayout()->createBlock(
            'core/template',
            'webform_' . $this->getWebform()->getId() . '_submit_script',
            array(
                'webform' => $this->getWebform(),
                'ajax' => $this->isAjax(),
                'template' => 'webforms/scripts/submit.phtml',
            )
        )->toHtml();
        ?>
        
    <?php
    } // if(show_success)
} // is active
?>